package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"time"

	api "gitee.com/lcc_dr/rpc/api"
	"gitee.com/lcc_dr/rpc/grpc/middleware"
	"google.golang.org/grpc"
)

func main() {
	ctx := context.Background()

	credential := middleware.NewGrpcAuther()
	credential.WithPerRPCCredentials("admin", "1234")
	grpcConn, err := grpc.DialContext(ctx, "127.0.0.1:1234", grpc.WithInsecure(), grpc.WithPerRPCCredentials(credential))
	if err != nil {
		panic(err)
	}

	grpcClient := api.NewHelloServiceClient(grpcConn)
	//
	// md := middleware.ClientCredentialsHeader("admin", "1234")
	// metactx := metadata.NewOutgoingContext(ctx, md)
	// CallOption := grpc.Header(header)
	// replay, err := grpcClient.Hello(metactx, &api.Request{Value: "world"})

	replay, err := grpcClient.Hello(ctx, &api.Request{Value: "world"})
	if err != nil {
		panic(err)
	}

	fmt.Println(replay)

	grpcStream, err := grpcClient.Channel(ctx)
	if err != nil {
		panic(err)
	}

	go func() {
		for {
			req := &api.Request{Value: "hello is"}
			err = grpcStream.Send(req)
			if err != nil {
				log.Fatal(err)
			}
			time.Sleep(time.Second)
		}
	}()

	for {
		replay, err = grpcStream.Recv()
		if err != nil {
			if err == io.EOF {
				break
			}
			log.Fatal(err)
		}

		fmt.Println(replay)
	}

}
